# -*- coding: utf-8 -*-
from datetime import timedelta
from airflow.operators.bash import BashOperator
from utils.operators.spark_submit import SparkSubmitOperator
from jms.time_sensor import time_after_01_30
from utils.alerts import dingding_task_alert
from jms.ods.tms.ods_tms_total_task_tag   import ods_tms_total_task_tag
from jms.ods.tab.ods_tab_total_task_tag   import ods_tab_total_task_tag
from jms.ods.oms.ods_oms_total_task_tag   import ods_oms_total_task_tag
from jms.ods.lmdm.ods_lmdm_total_task_tag import ods_lmdm_total_task_tag
from jms.ods.others.ods_others_total_task_tag import ods_others_total_task_tag

nowdt='{{ execution_date | cst_ds }}'
env_sync='{{ var.value.env_sync }}'

jdbcUrl1='{{ var.json.oracle_lmdm.url }}'
orausername1='{{ var.json.oracle_lmdm.username }}'
orapassword1='{{ var.json.oracle_lmdm.password }}'
jdbcUrl2='{{ var.json.oracle_bigdata_19c51.url }}'
orausername2='{{ var.json.oracle_bigdata_19c51.username }}'
orapassword2='{{ var.json.oracle_bigdata_19c51.password }}'
mysqlUrl='{{ var.json.mysql_personas.url }}'
mysqlusername='{{ var.json.mysql_personas.username }}'
mysqlpassword='{{ var.json.mysql_personas.password }}'

jsonpara="""{
"oracle":{
    "connect1":{
         "url":"jdbcUrl1",
         "username":"orausername1",
         "password":"orapassword1",
         "driver":"oracle.jdbc.driver.OracleDriver"},
    "connect2":{
         "url":"jdbcUrl2",
         "username":"orausername2",
         "password":"orapassword2",
         "driver":"oracle.jdbc.driver.OracleDriver"},
    "connect3":{
         "url":"mysqlUrl",
         "username":"mysqlusername",
         "password":"mysqlpassword",
         "driver":"com.mysql.cj.jdbc.Driver"},
},
"settting":{
    "env":"env_sync",
    "dt" :"nowdt"},
"table":{
    "tab_bagbillcode":"inputtime,na,tab_bagbillcode,connect2,1",
    "tab_barscan_arrival":"inputtime,na,tab_barscan_arrival,connect2,1",
    "tab_barscan_bagging":"inputtime,na,tab_barscan_bagging,connect2,1",
    "tab_barscan_centerarrival":"inputtime,na,tab_barscan_centerarrival,connect2,1",
    "tab_barscan_centersend":"inputtime,na,tab_barscan_centersend,connect2,1",
    "tab_barscan_collect":"inputtime,na,tab_barscan_collect,connect2,1",
    "tab_barscan_deliver":"inputtime,na,tab_barscan_deliver,connect2,1",
    "tab_barscan_difficult":"inputtime,na,tab_barscan_difficult,connect2,1",
    "tab_barscan_loading":"inputtime,na,tab_barscan_loading,connect2,1",
    "tab_barscan_noparsepackage":"inputtime,na,tab_barscan_noparsepackage,connect2,1",
    "tab_barscan_other":"inputtime,na,tab_barscan_other,connect2,1",
    "tab_barscan_send":"inputtime,na,tab_barscan_send,connect2,1",
    "tab_barscan_sign":"inputtime,na,tab_barscan_sign,connect2,1",
    "tab_barscan_signrep":"inputtime,na,tab_barscan_signrep,connect2,1",
    "tab_statistics":"create_time,na,tab_statistics,connect1,1",
    "tab_barscan_sitearrival":"inputtime,na,tab_barscan_sitearrival,connect2,1",
    "tab_barscan_sitesend":"inputtime,na,tab_barscan_sitesend,connect2,1",
    "tab_barscan_taking":"inputtime,na,tab_barscan_taking,connect2,1",
    "tab_barscan_unloading":"inputtime,na,tab_barscan_unloading,connect2,1",
    "tab_barscan_warehousing":"inputtime,na,tab_barscan_warehousing,connect2,1",
    "tab_sitearrivalmonitor":"inputtime,na,tab_sitearrivalmonitor,connect2,1",
    "tab_sitedelivermonitor":"inputtime,na,tab_sitedelivermonitor,connect2,1",
    "tab_sitesendmonitor":"inputtime,na,tab_sitesendmonitor,connect2,1",
    "tab_sitetakingmonitor":"inputtime,na,tab_sitetakingmonitor,connect2,1",
    "tab_barscan_notallow":"inputtime,na,tab_barscan_notallow,connect2,1",
    "tab_bulk_cargo_shift":"na,na,tab_bulk_cargo_shift,connect1,4",
    "tab_end_piece":"input_time,na,tab_end_piece,connect1,3",
    "tab_reback_transfer_express":"apply_time,na,tab_reback_transfer_express,connect1,3",
    "tab_aviation_send_record":"send_scan_time,na,tab_aviation_send_record,connect2,3",  
    "tab_booking_space":"na,na,tab_booking_space,connect2,4",  
    "tab_send_goods_report":"na,na,tab_send_goods_report,connect2,4",  
    "tab_take_delivery":"na,na,tab_take_delivery,connect2,4",  
    "tab_report_errorbagging_detail":"scan_time,na,tab_report_errorbagging_detail,connect2,1", 
    "tab_barscan_return_msg":"na,na,tab_barscan_return_msg,connect2,4",
    "tab_agent_point":"na,na,tab_agent_point,connect2,4",
    "tab_terminal_secret":"na,na,tab_terminal_secret,connect2,4",
    "tab_customer_monitor_deploy":"na,na,tab_customer_monitor_deploy,connect1,4",
    "tab_customer_monitor_vips":"na,na,tab_customer_monitor_vips,connect1,4",
    "yl_oms_oms_order":"input_time,na,yl_oms_oms_order,connect2,3",
    "yl_oms_oms_waybill":"input_time,na,yl_oms_oms_waybill,connect2,3",
    "yl_oms_oms_order_back_detail":"create_time,na,yl_oms_oms_order_back_detail,connect1,3",
    "yl_oms_interceptorpiece":"scan_time,na,yl_oms_interceptorpiece,connect2,3",
    "electronic_package_list":"create_time,na,electronic_package_list,connect2,3",
    "second_package_list":"create_time,na,second_package_list,connect2,3",
    "yl_lmdm_sys_abnormal_piece":"na,na,yl_lmdm_sys_abnormal_piece,connect1,4",
    "yl_lmdm_sys_area":"na,na,yl_lmdm_sys_area,connect1,4",
    "yl_lmdm_sys_article_type":"na,na,yl_lmdm_sys_article_type,connect1,4",
    "yl_lmdm_sys_customer":"na,na,yl_lmdm_sys_customer,connect1,4",
    "yl_lmdm_sys_dictionary":"na,na,yl_lmdm_sys_dictionary,connect1,4",
    "yl_lmdm_sys_first_code":"na,na,yl_lmdm_sys_first_code,connect1,4",
    "yl_lmdm_sys_network":"na,na,yl_lmdm_sys_network,connect1,4",
    "yl_lmdm_sys_network_delivery_r":"na,na,yl_lmdm_sys_network_delivery_r,connect1,4",
    "yl_lmdm_sys_network_distributi":"na,na,yl_lmdm_sys_network_distributi,connect1,4",
    "yl_lmdm_sys_network_electronic":"na,na,yl_lmdm_sys_network_electronic,connect1,4",
    "yl_lmdm_sys_network_expand":"na,na,yl_lmdm_sys_network_expand,connect1,4",
    "yl_lmdm_sys_payment_man":"na,na,yl_lmdm_sys_payment_man,connect1,4",
    "yl_lmdm_sys_product_type":"na,na,yl_lmdm_sys_product_type,connect1,4",
    "yl_lmdm_sys_scanning_type":"na,na,yl_lmdm_sys_scanning_type,connect1,4",
    "yl_lmdm_sys_second_code":"na,na,yl_lmdm_sys_second_code,connect1,4",
    "yl_lmdm_sys_settlement_dest":"na,na,yl_lmdm_sys_settlement_dest,connect1,4",
    "yl_lmdm_sys_staff":"na,na,yl_lmdm_sys_staff,connect1,4",
    "yl_lmdm_sys_transport_manner":"na,na,yl_lmdm_sys_transport_manner,connect1,4",
    "yl_lmdm_sys_tail_code":"na,na,yl_lmdm_sys_tail_code,connect1,4",
    "yl_lmdm_sys_tail_code_staff":"na,na,yl_lmdm_sys_tail_code_staff,connect1,4",
    "yl_lmdm_sys_user":"na,na,yl_lmdm_sys_user,connect1,4",
    "yl_lmdm_sys_user_role":"na,na,yl_lmdm_sys_user_role,connect1,4",
    "yl_lmdm_sys_third_code":"na,na,yl_lmdm_sys_third_code,connect1,4",
    "yl_lmdm_sys_special_time_limit":"na,na,yl_lmdm_sys_special_time_limit,connect1,4",
    "yl_tmsnew_tms_in_warehouse_shi":"create_time,na,yl_tmsnew_tms_in_warehouse_shi,connect1,3",
    "yl_tmsnew_tms_line_planning":"create_time,na,yl_tmsnew_tms_line_planning,connect1,3",
    "yl_tmsnew_tms_line_stop_point":"create_time,na,yl_tmsnew_tms_line_stop_point,connect1,3",
    "yl_tmsnew_tms_out_warehouse_sh":"create_time,na,yl_tmsnew_tms_out_warehouse_sh,connect1,3",
    "yl_tmsnew_tms_shipment":"create_time,na,yl_tmsnew_tms_shipment,connect1,3",
    "yl_tmsnew_tms_shipment_stop":"create_time,na,yl_tmsnew_tms_shipment_stop,connect1,3",
    "yl_tmsnew_tms_track":"create_time,na,yl_tmsnew_tms_track,connect1,3",
    "yl_tmsnew_tms_transfer_shift":"create_time,na,yl_tmsnew_tms_transfer_shift,connect1,3",
    "yl_tmsnew_tms_vehicle":"create_time,na,yl_tmsnew_tms_vehicle,connect1,3",
    "yl_tmsnew_tms_vehicle_point":"create_time,na,yl_tmsnew_tms_vehicle_point,connect1,3",
    "yl_tms_branch_delivery_network":"na,na,yl_tms_branch_delivery_network,connect1,4",
    "yl_tms_branch_line":"na,na,yl_tms_branch_line,connect1,4",
    "yl_tms_branch_point":"na,na,yl_tms_branch_point,connect1,4",
    "yl_tms_branch_time_effective":"na,na,yl_tms_branch_time_effective,connect1,4",
    "yl_tms_main_delivery_network":"na,na,yl_tms_main_delivery_network,connect1,4",
    "yl_tmsnew_tms_road_section":"na,na,yl_tmsnew_tms_road_section,connect1,4",
    "yl_tmsnew_tms_vehicle_line":"na,na,yl_tmsnew_tms_vehicle_line,connect1,4",
    "yl_tms_cs_shift":"na,na,yl_tms_cs_shift,connect1,4",
    "yl_tms_route":"na,na,yl_tms_route,connect2,4",
    "yl_tmsnew_branch_shipment":"create_time,na,yl_tmsnew_branch_shipment,connect1,3",
    "yl_tmsnew_branch_shipment_stop":"create_time,na,yl_tmsnew_branch_shipment_stop,connect1,3",
    "yl_tms_branch_shipment":"lock_time,na,yl_tms_branch_shipment,connect1,3",
    "yl_tmsnew_download_effective":"create_time,na,yl_tmsnew_download_effective,connect1,3",
    "yl_tmsnew_time_effective":"create_time,na,yl_tmsnew_time_effective,connect1,3",
    "yl_tms_vehicle_type":"na,na,yl_tms_vehicle_type,connect2,4",
    "yl_css.arbitration":"create_time,update_time,arbitration,connect3,5",
    "yl_css.project_work_order":"create_time,update_time,project_work_order,connect3,5",
    "yl_css.problem_express_wo":"create_time,update_time,problem_express_wo,connect3,5",
    "yl_css.claim_work_order":"create_time,update_time,claim_work_order,connect3,5",
    "yl_postal.complaint_info":"create_time,update_time,complaint_info,connect3,5",
    "yl_postal.offline_complaint":"create_time,update_time,offline_complaint,connect3,5",
    "yl_postal.appeal_info":"create_time,update_time,appeal_info,connect3,5"
    }
}""".replace("jdbcUrl1",jdbcUrl1).replace("orausername1",orausername1).replace("orapassword1",orapassword1). \
    replace("jdbcUrl2",jdbcUrl2).replace("orausername2",orausername2).replace("orapassword2",orapassword2). \
    replace("mysqlUrl",mysqlUrl).replace("mysqlusername",mysqlusername).replace("mysqlpassword",mysqlpassword). \
    replace("nowdt",nowdt). \
    replace("env_sync",env_sync)

jms_ods__oracle_hive_data__check = SparkSubmitOperator(
    task_id='jms_ods__oracle_hive_data__check',
    email='chenhongping@yl-scm.com',
    name='jms_ods__oracle_hive_data__check_{{ execution_date | cst_ds }}',
    priority_weight=0,
    pool_slots=2,
    execution_timeout=timedelta(hours=2),
    retries=1,
    driver_memory='2G',
    executor_memory='3G',
    executor_cores=2,
    num_executors=9,
    java_class='com.yunlu.bigdata.jobs.checkdata.Ora2Hive2DorisCheckData',              # spark 主类
    # application='hdfs:///user/hive/work/checkdata/check_oracle_hive.jar',               # spark jar 包
    application='hdfs:///scheduler/jms/spark/sync/check/check_oracle_hive.jar',               # spark jar 包
    application_args=[jsonpara,],
    on_failure_callback=dingding_task_alert(dingding_conn_id='dingding_bigdata'),
)

jms_ods__get_allfile_size__check = BashOperator(
    task_id='jms_ods__get_allfile_size__check',
    email='chenhongping@yl-scm.com',
    bash_command="jms/ods/check/oracle_hive_data_check/get_file_size.sh",
    pool='unlimited_pool',
    priority_weight=0,
)

jms_ods__oracle_hive_data__check << jms_ods__get_allfile_size__check

jms_ods__get_allfile_size__check << [
    time_after_01_30 ,
    ods_tms_total_task_tag,
    ods_tab_total_task_tag,
    ods_oms_total_task_tag,
    ods_lmdm_total_task_tag,
    ods_others_total_task_tag,
]
